//麻将算法
module.exports = function() {
  var HU_1 = 1001  //平胡
  var HU_2 = 1002  //七小对
  var HU_3 = 1003  //十三幺
  var local = {}
  this.cards = []           //初始牌库
  this.playerNum = 4        //玩家人数
  //洗牌
  this.shuffle = function() {
    this.cards = []
    for(var i = 1;i <= 9;i++){
      for(var j = 0;j < 4;j++){
        this.cards.push({type : 0,num : i})    //万
        this.cards.push({type : 1,num : 10+i})    //饼
        this.cards.push({type : 2,num : 20+i})    //条
      }
    }
    for(var i = 1;i <= 4;i++){
      this.cards.push({type : 4,num : 40})    //中
      this.cards.push({type : 4,num : 41})    //发
      this.cards.push({type : 4,num : 42})    //白
      this.cards.push({type : 5,num : 51})    //东
      this.cards.push({type : 5,num : 52})    //南
      this.cards.push({type : 5,num : 53})    //西
      this.cards.push({type : 5,num : 54})    //北
    }
    for(var i = 1;i <= 8;i++){
      this.cards.push({type : 6,num : 60+i})    //春夏秋冬梅兰菊竹
    }
    this.cards.sort(function(a, b) {return Math.random()>.5 ? -1 : 1})
  }
  //发牌
  this.sendCard = function() {
    this.shuffle()
    var handCardList = []
    for(var i = 0;i < this.playerNum;i++){
      handCardList[i] = this.cards.splice(0,14)
    }
    return handCardList
  }
  //检测胡牌
  this.checkHu = function(handCard) {
    handCard.sort(function(a, b) {if(a.type > b.type || (a.type == b.type && a.num >= b.num)){return 1}else{return -1}})
    if(this.thirteen(handCard)){
      return true
    }
    if(this.qiDui(handCard)){
      return true
    }
    if(this.basicHu(handCard)){
      return true
    }
    return false
  }
  //十三幺
  this.thirteen = function(handCard) {
    if(handCard.length != 14){
      return false
    }
    var list = {
      "1" : 1,
      "9" : 1,
      "11" : 1,
      "19" : 1,
      "21" : 1,
      "29" : 1,
      "40" : 1,
      "41" : 1,
      "42" : 1,
      "51" : 1,
      "52" : 1,
      "53" : 1,
      "54" : 1,
    }
    for(var i = 0;i < handCard.length;i++){
      if(!list[handCard[i].num]){
        return false
      }
      list[handCard[i].num] = 2
    }
    for(var i in list){
      if(list[i] != 2){
        return false
      }
    }
    return true
  }
  //七对胡牌检测
  this.qiDui = function(handCard) {
    if(handCard.length != 14){
      return false
    }
    var count = 0
    for(var i = 0;i < handCard.length - 1;i += 2){
      if(handCard[i].type != 6 && handCard[i].type == handCard[i + 1].type && handCard[i].num == handCard[i + 1].num){
        count++
      }
    }
    if(count == 7){
      return true
    }else{
      return false
    }
  }
  //基础胡牌检测
  this.basicHu = function(handCard) {
    if(!handCard){
      return false
    }
    handCard.sort(function(a, b) {if(a.type > b.type || (a.type == b.type && a.num >= b.num)){return 1}else{return -1}})
    console.log("原始牌组",handCard)
    for(var i = 0;i < handCard.length - 1;i++){
      if(handCard[i].type == handCard[i+1].type && handCard[i].num == handCard[i+1].num){
        var tmpHandList = []
        for(var j = 0;j < handCard.length;j++){
          if(j != i && j != i+1){
            tmpHandList.push(handCard[j])
          }
        }
        console.log("眼 : ",handCard[i])
        console.log("剩余牌组 : ",tmpHandList)
        local.check_3n(tmpHandList)
        if(tmpHandList.length == 0){
          console.log("胡牌")
          return true
        }
      }
    }
    return false
  }
  local.check_3n = function(handCard) {
    console.log("check_3n")
    console.log(handCard)
    if(!handCard.length){
      return true
    }
    var flag = false
    if(local.remove_three_same(handCard)){
      flag = true
    }
    if(local.remove_straight(handCard)){
      flag = true
    }
    if(flag){
      local.check_3n(handCard)
    }
  }
  local.remove_three_same = function(handCard) {
    if(handCard.length < 3){
      return false
    }
    if(handCard[0].type != 6 && 
      handCard[0].num == handCard[1].num && handCard[1].num == handCard[2].num &&
      handCard[0].type == handCard[1].type && handCard[1].type == handCard[2].type){
      // console.log("去除刻子",handCard[0])
      handCard.splice(0,3)
      return true
    }
    return false
  }
  local.remove_straight = function(handCard) {
    if(handCard.length < 3){
      return false
    }
    var list = {}
    list[handCard[0].num + 1] = false
    list[handCard[0].num + 2] = false
    var tmpList = []
    for(var i = 1;i < handCard.length;i++){
      if(handCard[i].type >= 0 && handCard[i].type <= 2 && handCard[i].type == handCard[0].type){
        if(handCard[i].num == handCard[0].num + 1 || handCard[i].num == handCard[0].num + 2){
          list[handCard[i].num] = true
        }
      }
    }
    if(list[handCard[0].num + 1] && list[handCard[0].num + 2]){
      for(var i = 1;i < handCard.length;i++){
        if(handCard[i].type == handCard[0].type && handCard[i].num == handCard[0].num + 1){
          handCard.splice(i,1)
          break
        }
      }
      for(var i = 1;i < handCard.length;i++){
        if(handCard[i].type == handCard[0].type && handCard[i].num == handCard[0].num + 2){
          handCard.splice(i,1)
          break
        }
      }
      handCard.splice(0,1)
      // console.log("去除顺子")
      return true
    }
    return false
  }
}
